using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

namespace TestSystem.DAL.TestSystem
{
    public class AnswerDataAccess : DataAccess
    {
        public static IDataReader GetAnswersByTestID(int testID)
        {
            SqlParameter [] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID)     
                };
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answer_GetByTestID", paramsArray);
        }

        public static IDataReader GetCorrectAnswersByTestID(int testID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID)     
                };
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answer_GetCorrectByTestID", paramsArray);
        }

        public static int Insert(int testID, string body, int answerOrder, bool isCorrect)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID),
                    new SqlParameter("@Body", SqlDbType.NText, NTEXT, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, body),
                    new SqlParameter("@AnswerOrder", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, answerOrder),
                    new SqlParameter("@IsCorrect", SqlDbType.Bit, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, isCorrect)                
                };
            return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Answer_Insert", paramsArray));
        }

        public static int Insert(SqlTransaction tran, int testID, string body, int answerOrder, bool isCorrect)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID),
                    new SqlParameter("@Body", SqlDbType.NText, NTEXT, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, body),
                    new SqlParameter("@AnswerOrder", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, answerOrder),
                    new SqlParameter("@IsCorrect", SqlDbType.Bit, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, isCorrect)                
                };
            return Convert.ToInt32(SqlHelper.ExecuteScalar(tran, CommandType.StoredProcedure, "Answer_Insert", paramsArray));
        }

        public static void DeleteAnswersByTestID(int testID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID)     
                };
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Answer_DeleteByTestID", paramsArray);
        }

        public static int GetAnswersCountByTestID(int testID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID)     
                };
            return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Answer_GetCountByTestID", paramsArray));
        }

        public static int GetCorrectAnswersCountByTestID(int testID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID)     
                };
            return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Answer_GetCorrectCountByTestID", paramsArray));
        }

        public static void Update(int answerID, int testID, string body, int answerOrder, bool isCorrect)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@TestID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, testID),
                    new SqlParameter("@Body", SqlDbType.NText, NTEXT, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, body),
                    new SqlParameter("@AnswerOrder", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, answerOrder),
                    new SqlParameter("@IsCorrect", SqlDbType.Bit, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, isCorrect),
                    new SqlParameter("@AnswerID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, answerID) 
                };
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Answer_Update", paramsArray);
        }

        public static void Delete(int answerID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@AnswerID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, answerID)     
                };
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Answer_Delete", paramsArray);
        }

        public static IDataReader GetAll()
        {
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answer_GetAll");
        }

        public static IDataReader GetByAnswerID(int answerID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@AnswerID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, answerID)     
                };
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answer_GetByAnswerID", paramsArray);
        }

        public static IDataReader GetByQuizID(int quizID)
        {
            SqlParameter[] paramsArray = new SqlParameter[]
                {
                    new SqlParameter("@QuizID", SqlDbType.Int, 0, ParameterDirection.Input, true, 10, 0, null, DataRowVersion.Current, quizID)     
                };
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answer_GetByQuizID", paramsArray);
        }

        public static int GetCount()
        {
            return Convert.ToInt32(SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Answer_GetCount"));
        }
    }
}
